package com.ubuntuvim.threadframework.client;

import java.util.Stack;

import com.ubuntuvim.threadframework.MethodRequest;

/**
 * 主要用来存放调用者请求
 * @author cdq@fortunes.com.cn
 */
public class ActiveQueue {
	private Stack queue;
	private final static int QUEUE_SIZE = 20;
	
	public ActiveQueue() {
		queue = new Stack();
	}
	
	public synchronized void enqueue(MethodRequest mr) {
		while (queue.size() > QUEUE_SIZE) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			queue.push(mr);
			notifyAll();
			System.out.println("leave dequeue...");
		}
	}
	
	public synchronized MethodRequest dequeue() {
		MethodRequest mr = null;
		while (queue.empty()) {  // 队列为空，等待
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		mr = (MethodRequest) queue.pop();
		notifyAll();
		
		return mr;
	}
}
